import com.sun.source.tree.Tree;

import javax.swing.tree.TreeNode;

public class SearchTree {
    public static void great(TreeNode root){

        TreeNode a = new TreeNode(30);
        TreeNode b = new TreeNode(20);
        TreeNode c = new TreeNode(50);
        TreeNode d = new TreeNode(40);
        TreeNode e = new TreeNode(60);
        TreeNode f = new TreeNode(45);
        TreeNode g = new TreeNode(55);
        root.left = b;
        root.right = c;
        c.left = d;
        c.right = e;
        d.right = f;
        e.right = g;
    }
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }
        //搜索某一值在哪个节点上
    public static boolean search(TreeNode root,int val){
        TreeNode cur = root;
        if (root == null){
            return false;
        }
        while (cur != null){
            if (cur.val < val){
                cur = cur.right;
            }
            if (cur.val > val){
                cur = cur.left;
            }
            if (cur.val == val){
                return true;
            }
        }
        return false;
    }

    //搜索树的插入
    public boolean insert(TreeNode root,int val){
        if (root == null){
            root = new TreeNode(val);
            return true;
        }
        TreeNode cur = root;
        TreeNode par = null;
        while (cur != null){
            if (cur.val < val){
                par = cur;
                cur = cur.right;
            }
            if (cur.val > val){
                par = cur;
                cur = cur.left;
            }
            if (cur.val == val){
                return false;
            }
        }
        if (par.val > val){
            par.left.val = val;
        }
        if (par.val < val){
            par.right.val = val;
        }
        return true;
    }

    //搜素树的删除
    public static void remove(TreeNode root, TreeNode parent, TreeNode cur){
        //当 cur 节点的左节点为空
        if (cur.left == null){
            if (cur == root){
                root = root.right;
            }
            if (cur == parent.left){
                parent.left = cur.right;
            }
            if (cur == parent.right){
                parent.right = cur.right;
            }
        }
        //当 cur 节点的右节点为空
        if (cur.right == null){
            if (cur == root){
                cur = cur.left;
            }
            if (cur == parent.left){
                parent.left = cur.left;
            }
            if (cur == parent.right){
                parent.right = cur.left;
            }
        }else {
            //当 cur 节点两边不为空
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while (target.left != null){
                targetParent = parent;
                target = target.left;
            }
            cur.val = target.val;
            if (targetParent.left == parent){
                targetParent.left = parent.right;
            }
            if (targetParent.right == targetParent){
                targetParent.right = parent.right;
            }
        }

    }
}
